﻿using UnityEditor;
using UnityEngine;
using System.IO;
using ZyGame.Editor.AssetBuilder;
using ZyGame.Editor.Process;
using ZyGame.Editor.CloudBuilder;
using ZyGame.Editor.BuilderProcess;
using ZyGame.Editor.RPC;
using ZyGame.Editor.UIBind;

namespace ZyGame.Editor
{
    public class ZyEditorWindow : EditorWindow
    {
        private Vector2 scroll;
        public void OnGUI()
        {
            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal(EditorStyles.toolbar);
            OnDrawToobar();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            OnDrawRightTree();
            scroll = GUILayout.BeginScrollView(scroll);
            OnDrawDataView();
            GUILayout.EndScrollView();
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            OnDrawBottom();
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
        }
        protected virtual void OnDrawToobar() { }

        protected virtual void OnDrawRightTree() { }

        protected virtual void OnDrawDataView() { }

        protected virtual void OnDrawBottom() { }
    }


    public static class EditorExtension
    {
        public static void CheckMouseDragEvent(this EditorWindow window, Rect rect)
        {
            if (Rect.zero.Equals(rect) is not true && rect.Contains(UnityEngine.Event.current.mousePosition) is not true)
            {
                return;
            }
            if (UnityEngine.Event.current.type is not EventType.DragUpdated)
            {
                return;
            }
            DragAndDrop.visualMode = DragAndDropVisualMode.Link;
        }

        public static bool CheckMouseDragdropEvent(this EditorWindow window, Rect rect)
        {
            if (Rect.zero.Equals(rect) is not true && rect.Contains(UnityEngine.Event.current.mousePosition) is not true)
            {
                return false;
            }
            if (UnityEngine.Event.current.type is not EventType.DragPerform)
            {
                return false;
            }
            return true;
        }

        public static bool CheckMouseClickEvent(this EditorWindow window, Rect rect)
        {
            if (rect.Contains(UnityEngine.Event.current.mousePosition) is not true)
            {
                return false;
            }
            if (UnityEngine.Event.current.type is not EventType.MouseDown)
            {
                return false;
            }
            return true;
        }
    }

    internal class MenuCommand
    {
        [MenuItem("ZyGame/Editor/UI Create")]
        public static void OpenUIGenerate()
        {
            EditorWindow.GetWindow<UICreateWindow>(false, "UI Create", true);
        }

        [MenuItem("ZyGame/Command/Protocol Build")]
        public static void BuildProtoBuf()
        {
            RPCBuild.OnStartBuild();
        }

        [MenuItem("ZyGame/Editor/Colud Build Service")]
        public static void OpenColudBuildService()
        {
            EditorWindow.GetWindow<CloudBuildService>(true, "Colud Build Service - " + EditorUserBuildSettings.activeBuildTarget, false);
        }

        [MenuItem("ZyGame/Command/Import Config(Excel)")]
        public static void OpenColudBuildClient()
        {
            ImportExcelConfigPorcess.Import();
        }

        [MenuItem("ZyGame/Editor/Avatar Element Build")]
        public static void OpenAvatarElementWindow()
        {
            EditorWindow.GetWindow<Avatar.AvatarBuilderEditorWindow>(true, "Element Builder", true);
        }

        [MenuItem("ZyGame/Options")]
        public static void OpenSetting()
        {
            SettingsService.OpenProjectSettings("Project/Options");
        }

        [MenuItem("ZyGame/Editor/Asset Builder")]
        public static void Open()
        {
            EditorWindow.GetWindow<PackageBuilderEditorWindow>(false, "Asset Builder", true);
        }

        [MenuItem("ZyGame/Command/Clear All Data")]
        public static void ClearCacheData()
        {
            ClearEditorCacheData();
            ClearPlayerCacheData();
            ClearVFSCacheData();
        }

        [MenuItem("ZyGame/Command/Clear File Data")]
        public static void ClearVFSCacheData()
        {
            string localtion = Application.persistentDataPath + "/" + AppConfig.instance.gameOptions.bucket_name;
            if (Directory.Exists(localtion))
            {
                Directory.Delete(localtion, true);
            }
        }

        [MenuItem("ZyGame/Command/Clear Player Data")]
        public static void ClearPlayerCacheData()
        {
            PlayerPrefs.DeleteAll();
        }

        [MenuItem("ZyGame/Command/Clear Editor Data")]
        public static void ClearEditorCacheData()
        {
            EditorPrefs.DeleteAll();
        }

        [MenuItem("Assets/ZyGame/GameObject/Create Texture Material")]
        public static void SetSeletionGameObjectMaterial()
        {
            CreateGameObjectMaterialProcess.Running();
        }

        [MenuItem("GameObject/Copy Path", false, 2)]
        public static void CopyPath()
        {
            CopySeletionGameObjectPathProcess.Running();
        }

        [MenuItem("Assets/ZyGame/Copy Path")]
        public static void CopySeletionPath()
        {
            if (Selection.objects is null || Selection.objects.Length <= 0)
            {
                return;
            }
            string path = AssetDatabase.GetAssetPath(Selection.objects[0]);

            TextEditor text = new TextEditor();
            text.text = path;
            text.SelectAll();
            text.Copy();
        }
    }
}
